$(function() {
    // alert(11)
    $('.icon-arrow-left').on('click', function() {
            location.href = './address.html'
        })
        //获取数据
    getEdit()

    function getEdit() {
        const id = localStorage.getItem('id')
        $.ajax({
            method: 'get',
            url: 'http://121.4.102.44:8083/addressBook/' + id,
            success(res) {
                console.log(res);
                $('.hlq_name').val(res.data.consignee)
                $('.hlq_phone').val(res.data.phone)
                $('.hlq_address').val(res.data.detail)
                    // const Laber = $('.hlq_laber .spanItem').html()
                    // if (res.data.label === Laber) {

                // }
                // Laber.forEach(function(item) {
                //     console.log(item.html());
                // });
                //console.log(Laber);
                if (res.data.label == '') {
                    $('.hlq_laber .spanItem').eq(0).addClass('')
                } else if (res.data.label == '公司') {
                    $('.hlq_laber .spanItem').eq(1).addClass('spanActiveSchool')
                } else if (res.data.label == '家') {
                    $('.hlq_laber .spanItem').eq(2).addClass('spanActiveSchool')
                } else if (res.data.label == '学校') {
                    $('.hlq_laber .spanItem').eq(3).addClass('spanActiveSchool')
                }
                let sex = 0
                let label = ''
                $('.spanChecked i').on('click', function() {
                    $(this).addClass('iActive').parent().siblings('.spanChecked').children('i').removeClass('iActive')
                    let num = $('i.iActive').parent().text().trim()
                        //console.log(num);
                    if (num === '先生') {
                        sex = 1
                    } else if (num === '女士') {
                        sex = 0
                    }
                    //console.log(sex);
                })
                $('.hlq_laber span').on('click', function() {
                        $(this).addClass('spanActiveSchool').siblings().removeClass('spanActiveSchool')
                        label = $('span.spanActiveSchool').html().trim()
                            //console.log(label);
                    })
                    //修改数据
                $('#hlq_divSave').on('click', function() {
                    const id = localStorage.getItem('id')
                    const useId = res.data.useId
                    const consignee = $('.hlq_name').val().trim()
                    const detail = $('.hlq_address').val().trim()
                    const phone = $('.hlq_phone').val().trim()
                    let reg1 = /^1[3|4|5|6|7|8|9]\d{9}$/
                    if (consignee === '' || detail === '' || phone === '') {
                        return layui.layer.msg('请填写完整', { icon: 5 })
                    }
                    if (!reg1.test(phone)) {
                        return layui.layer.msg('请输入正确的手机格式', { icon: 5, time: 1500 })
                    }

                    $.ajax({
                        method: 'put',
                        url: 'http://121.4.102.44:8083/addressBook',
                        data: JSON.stringify({
                            id,
                            useId,
                            consignee,
                            detail,
                            label,
                            phone,
                            sex
                        }),
                        contentType: "application/json",
                        success(res) {
                            //console.log(res);
                            if (res.code !== 1) {
                                return layui.layer.msg('修改失败', { icon: 5 })
                            }
                            layui.layer.msg('修改成功', { icon: 6, time: 1500 }, function() {
                                location.href = './address.html'
                            })

                        }
                    })
                })
            }
        })

    }

    //删除地址
    $('#hlq_deladdress').on('click', function() {
        const id = localStorage.getItem('id')
        $('.mask').show()
        $('.mask_btm i').eq(0).on('click', function() {
            $('.mask').hide()
        })
        $('.mask_btm i').eq(1).on('click', function() {
            $.ajax({
                method: 'DELETE',
                url: 'http://121.4.102.44:8083/addressBook?ids=' + id,
                success(res) {
                    console.log(res);
                    if (res.code !== 1) {
                        return layui.layer.msg('删除失败', { icon: 5 })
                    }
                    layui.layer.msg('删除成功', {
                        icon: 6,
                        time: 2000
                    }, function() {
                        location.href = './address.html'
                    })
                }
            })
        })

    })
})